package com.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dao.DepotDao;
import com.dao.DepotDao2;
import com.models.Depot;
import com.models.Depot2;

@Controller
@RequestMapping("/depot")//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
public class DepotController {
	
	@Autowired
	private DepotDao depotDao;
	
	@Autowired
	private DepotDao2 depotDao2;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView getAll(){
		ModelAndView mav = new ModelAndView("depot/depotList");//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
		List<Depot> depots = depotDao.findAll();
		mav.addObject("depots",depots);//eeeeeeeeeeeeeeeeeeeeeeeeeeeee
		
		return mav;
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public ModelAndView depot(@PathVariable int id){
		ModelAndView mav = new ModelAndView("depot/depot");
		Depot depot = depotDao.findById(id);
		mav.addObject("depot",depot);//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
		return mav;
	}
	
	/*@RequestMapping("/{id}")
	public String get(@PathVariable int id){
		
		return "depot";
	}*/
	
	@RequestMapping(value="/depotList2",method=RequestMethod.GET)
	public ModelAndView depotList(){
		ModelAndView mav = new ModelAndView("depot/depotList2");//eeeeeeeeeeeeeeeeeeeeeeeeeeeeee
		List<Depot2> depots2 = depotDao2.findAll();
		mav.addObject("depots2",depots2);
		return mav;
	}
	
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav = new ModelAndView("depot/add");//eeeeeeeeeeeeeeeeeeeeeeeeeeeeee
		return mav;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(@RequestParam String name,@RequestParam String price,@RequestParam int amount,@RequestParam String remark){
		Depot depot = new Depot();
		depot.setName(name);
		depot.setPrice(price);
		depot.setAmount(amount);
		depot.setRemark(remark);
		depotDao.add(depot);
		ModelAndView mav = new ModelAndView("redirect:/depot");//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
		return mav;
	}
	
	@RequestMapping(value="/add2",method=RequestMethod.GET)
	public ModelAndView add2(){
		ModelAndView mav = new ModelAndView("depot/add2");//eeeeeeeeeeeeeeeeeeeeeeeeeeeeee
		return mav;
	}
	
	@RequestMapping(value="/add2",method=RequestMethod.POST)
	public ModelAndView add2(@RequestParam String name,@RequestParam String price,@RequestParam int amount,@RequestParam String remark){
		Depot depot = new Depot();
		depot.setName(name);
		depot.setPrice(price);
		depot.setAmount(amount);
		depot.setRemark(remark);
		depotDao.add(depot);
		ModelAndView mav = new ModelAndView("redirect:/depot");//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
		return mav;
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ModelAndView delete(@RequestParam int id){
		depotDao.delete(id);
		
		ModelAndView mav = new ModelAndView("redirect:/depot");//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
		return mav;
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam int id){
		
		Depot depot =depotDao.findById(id);
		
		ModelAndView mav = new ModelAndView("depot/edit");//eeeeeeeeeeeeeeeeeeeeeeeeeeee
		mav.addObject("depot",depot);//eeeeeeeeeeeeeeeeeeeeeeeeeeeeee
		return mav;
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public ModelAndView edit(@RequestParam int id,@RequestParam String name,@RequestParam String price,@RequestParam int amount,@RequestParam String remark){
		
		Depot depot = new Depot();
		depot.setId(id);
		depot.setName(name);
		depot.setPrice(price);
		depot.setAmount(amount);
		depot.setRemark(remark);
		
		depotDao.modify(depot);
		ModelAndView mav = new ModelAndView("redirect:/depot");//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
		return mav;
	}
}
